﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyCompanyName.AbpZeroTemplate.Gantt
{
    [MetadataType(typeof(AssignmentMetadata))]
    public partial class Assignment
    {
        [JsonIgnore]
        [NotMapped]
        public string PhantomTaskId { get; set; }

        [NotMapped]
        public string TaskId
        {
            set
            {
                PhantomTaskId = value;
                if (value == null) return;

                try
                {
                    TaskIdRaw = Convert.ToInt32(value);
                    PhantomTaskId = value;
                }
                catch (System.Exception)
                {
                    TaskIdRaw = 0;
                }
            }

            get
            {
                return TaskIdRaw > 0 ? Convert.ToString(TaskIdRaw) : PhantomTaskId;
            }
        }

        [JsonIgnore]
        [NotMapped]
        public string PhantomResourceId { get; set; }

        [NotMapped]
        public string ResourceId
        {
            set
            {
                PhantomResourceId = value;
                if (value == null) return;

                try
                {
                    ResourceIdRaw = Convert.ToInt32(value);
                    PhantomResourceId = value;
                }
                catch (System.Exception)
                {
                    ResourceIdRaw = 0;
                }
            }

            get
            {
                return ResourceIdRaw > 0 ? Convert.ToString(ResourceIdRaw) : PhantomResourceId;
            }
        }
    }


    public class AssignmentMetadata
    {
        [JsonIgnore]
        public int TaskIdRaw { get; set; }
        [JsonIgnore]
        public int ResourceIdRaw { get; set; }
        [JsonIgnore]
        public virtual Resource Resource { get; set; }
        [JsonIgnore]
        public virtual Task Task { get; set; }
    }
}
